1   /*
2    * Copyright (C) 2006 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.base;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import junit.framework.AssertionFailedError;
22  import junit.framework.TestCase;
23  
24  /**
25   * Unit test for {@link Preconditions}.
26   *
27   * @author Kevin Bourrillion
28   * @author Jared Levy
29   */
30  @GwtCompatible(emulated = true)
31  public class PreconditionsTest extends TestCase {
32    public void testCheckArgument_simple_success() {
33      Preconditions.checkArgument(true);
34    }
35  
36    public void testCheckArgument_simple_failure() {
37      try {
38        Preconditions.checkArgument(false);
39        fail("no exception thrown");
40      } catch (IllegalArgumentException expected) {
41      }
42    }
43  
44    public void testCheckArgument_simpleMessage_success() {
45      Preconditions.checkArgument(true, IGNORE_ME);
46    }
47  
48    public void testCheckArgument_simpleMessage_failure() {
49      try {
50        Preconditions.checkArgument(false, new Message());
51        fail("no exception thrown");
52      } catch (IllegalArgumentException expected) {
53        verifySimpleMessage(expected);
54      }
55    }
56  
57    public void testCheckArgument_nullMessage_failure() {
58      try {
59        Preconditions.checkArgument(false, null);
60        fail("no exception thrown");
61      } catch (IllegalArgumentException expected) {
62        assertEquals("null", expected.getMessage());
63      }
64    }
65  
66    public void testCheckArgument_complexMessage_success() {
67      Preconditions.checkArgument(true, "%s", IGNORE_ME);
68    }
69  
70    public void testCheckArgument_complexMessage_failure() {
71      try {
72        Preconditions.checkArgument(false, FORMAT, 5);
73        fail("no exception thrown");
74      } catch (IllegalArgumentException expected) {
75        verifyComplexMessage(expected);
76      }
77    }
78  
79    public void testCheckState_simple_success() {
80      Preconditions.checkState(true);
81    }
82  
83    public void testCheckState_simple_failure() {
84      try {
85        Preconditions.checkState(false);
86        fail("no exception thrown");
87      } catch (IllegalStateException expected) {
88      }
89    }
90  
91    public void testCheckState_simpleMessage_success() {
92      Preconditions.checkState(true, IGNORE_ME);
93    }
94  
95    public void testCheckState_simpleMessage_failure() {
96      try {
97        Preconditions.checkState(false, new Message());
98        fail("no exception thrown");
99      } catch (IllegalStateException expected) {
100       verifySimpleMessage(expected);
101     }
102   }
103 
104   public void testCheckState_nullMessage_failure() {
105     try {
106       Preconditions.checkState(false, null);
107       fail("no exception thrown");
108     } catch (IllegalStateException expected) {
109       assertEquals("null", expected.getMessage());
110     }
111   }
112 
113   public void testCheckState_complexMessage_success() {
114     Preconditions.checkState(true, "%s", IGNORE_ME);
115   }
116 
117   public void testCheckState_complexMessage_failure() {
118     try {
119       Preconditions.checkState(false, FORMAT, 5);
120       fail("no exception thrown");
121     } catch (IllegalStateException expected) {
122       verifyComplexMessage(expected);
123     }
124   }
125 
126   private static final String NON_NULL_STRING = "foo";
127 
128   public void testCheckNotNull_simple_success() {
129     String result = Preconditions.checkNotNull(NON_NULL_STRING);
130     assertSame(NON_NULL_STRING, result);
131   }
132 
133   public void testCheckNotNull_simple_failure() {
134     try {
135       Preconditions.checkNotNull(null);
136       fail("no exception thrown");
137     } catch (NullPointerException expected) {
138     }
139   }
140 
141   public void testCheckNotNull_simpleMessage_success() {
142     String result = Preconditions.checkNotNull(NON_NULL_STRING, IGNORE_ME);
143     assertSame(NON_NULL_STRING, result);
144   }
145 
146   public void testCheckNotNull_simpleMessage_failure() {
147     try {
148       Preconditions.checkNotNull(null, new Message());
149       fail("no exception thrown");
150     } catch (NullPointerException expected) {
151       verifySimpleMessage(expected);
152     }
153   }
154 
155   public void testCheckNotNull_complexMessage_success() {
156     String result = Preconditions.checkNotNull(
157         NON_NULL_STRING, "%s", IGNORE_ME);
158     assertSame(NON_NULL_STRING, result);
159   }
160 
161   public void testCheckNotNull_complexMessage_failure() {
162     try {
163       Preconditions.checkNotNull(null, FORMAT, 5);
164       fail("no exception thrown");
165     } catch (NullPointerException expected) {
166       verifyComplexMessage(expected);
167     }
168   }
169 
170   public void testCheckElementIndex_ok() {
171     assertEquals(0, Preconditions.checkElementIndex(0, 1));
172     assertEquals(0, Preconditions.checkElementIndex(0, 2));
173     assertEquals(1, Preconditions.checkElementIndex(1, 2));
174   }
175 
176   public void testCheckElementIndex_badSize() {
177     try {
178       Preconditions.checkElementIndex(1, -1);
179       fail();
180     } catch (IllegalArgumentException expected) {
181       // don't care what the message text is, as this is an invalid usage of
182       // the Preconditions class, unlike all the other exceptions it throws
183     }
184   }
185 
186   public void testCheckElementIndex_negative() {
187     try {
188       Preconditions.checkElementIndex(-1, 1);
189       fail();
190     } catch (IndexOutOfBoundsException expected) {
191       assertEquals("index (-1) must not be negative", expected.getMessage());
192     }
193   }
194 
195   public void testCheckElementIndex_tooHigh() {
196     try {
197       Preconditions.checkElementIndex(1, 1);
198       fail();
199     } catch (IndexOutOfBoundsException expected) {
200       assertEquals("index (1) must be less than size (1)",
201           expected.getMessage());
202     }
203   }
204 
205   public void testCheckElementIndex_withDesc_negative() {
206     try {
207       Preconditions.checkElementIndex(-1, 1, "foo");
208       fail();
209     } catch (IndexOutOfBoundsException expected) {
210       assertEquals("foo (-1) must not be negative", expected.getMessage());
211     }
212   }
213 
214   public void testCheckElementIndex_withDesc_tooHigh() {
215     try {
216       Preconditions.checkElementIndex(1, 1, "foo");
217       fail();
218     } catch (IndexOutOfBoundsException expected) {
219       assertEquals("foo (1) must be less than size (1)",
220           expected.getMessage());
221     }
222   }
223 
224   public void testCheckPositionIndex_ok() {
225     assertEquals(0, Preconditions.checkPositionIndex(0, 0));
226     assertEquals(0, Preconditions.checkPositionIndex(0, 1));
227     assertEquals(1, Preconditions.checkPositionIndex(1, 1));
228   }
229 
230   public void testCheckPositionIndex_badSize() {
231     try {
232       Preconditions.checkPositionIndex(1, -1);
233       fail();
234     } catch (IllegalArgumentException expected) {
235       // don't care what the message text is, as this is an invalid usage of
236       // the Preconditions class, unlike all the other exceptions it throws
237     }
238   }
239 
240   public void testCheckPositionIndex_negative() {
241     try {
242       Preconditions.checkPositionIndex(-1, 1);
243       fail();
244     } catch (IndexOutOfBoundsException expected) {
245       assertEquals("index (-1) must not be negative", expected.getMessage());
246     }
247   }
248 
249   public void testCheckPositionIndex_tooHigh() {
250     try {
251       Preconditions.checkPositionIndex(2, 1);
252       fail();
253     } catch (IndexOutOfBoundsException expected) {
254       assertEquals("index (2) must not be greater than size (1)",
255           expected.getMessage());
256     }
257   }
258 
259   public void testCheckPositionIndex_withDesc_negative() {
260     try {
261       Preconditions.checkPositionIndex(-1, 1, "foo");
262       fail();
263     } catch (IndexOutOfBoundsException expected) {
264       assertEquals("foo (-1) must not be negative", expected.getMessage());
265     }
266   }
267 
268   public void testCheckPositionIndex_withDesc_tooHigh() {
269     try {
270       Preconditions.checkPositionIndex(2, 1, "foo");
271       fail();
272     } catch (IndexOutOfBoundsException expected) {
273       assertEquals("foo (2) must not be greater than size (1)",
274           expected.getMessage());
275     }
276   }
277 
278   public void testCheckPositionIndexes_ok() {
279     Preconditions.checkPositionIndexes(0, 0, 0);
280     Preconditions.checkPositionIndexes(0, 0, 1);
281     Preconditions.checkPositionIndexes(0, 1, 1);
282     Preconditions.checkPositionIndexes(1, 1, 1);
283   }
284 
285   public void testCheckPositionIndexes_badSize() {
286     try {
287       Preconditions.checkPositionIndexes(1, 1, -1);
288       fail();
289     } catch (IllegalArgumentException expected) {
290     }
291   }
292 
293   public void testCheckPositionIndex_startNegative() {
294     try {
295       Preconditions.checkPositionIndexes(-1, 1, 1);
296       fail();
297     } catch (IndexOutOfBoundsException expected) {
298       assertEquals("start index (-1) must not be negative",
299           expected.getMessage());
300     }
301   }
302 
303   public void testCheckPositionIndexes_endTooHigh() {
304     try {
305       Preconditions.checkPositionIndexes(0, 2, 1);
306       fail();
307     } catch (IndexOutOfBoundsException expected) {
308       assertEquals("end index (2) must not be greater than size (1)",
309           expected.getMessage());
310     }
311   }
312 
313   public void testCheckPositionIndexes_reversed() {
314     try {
315       Preconditions.checkPositionIndexes(1, 0, 1);
316       fail();
317     } catch (IndexOutOfBoundsException expected) {
318       assertEquals("end index (0) must not be less than start index (1)",
319           expected.getMessage());
320     }
321   }
322 
323   public void testFormat() {
324     assertEquals("%s", Preconditions.format("%s"));
325     assertEquals("5", Preconditions.format("%s", 5));
326     assertEquals("foo [5]", Preconditions.format("foo", 5));
327     assertEquals("foo [5, 6, 7]", Preconditions.format("foo", 5, 6, 7));
328     assertEquals("%s 1 2", Preconditions.format("%s %s %s", "%s", 1, 2));
329     assertEquals(" [5, 6]", Preconditions.format("", 5, 6));
330     assertEquals("123", Preconditions.format("%s%s%s", 1, 2, 3));
331     assertEquals("1%s%s", Preconditions.format("%s%s%s", 1));
332     assertEquals("5 + 6 = 11", Preconditions.format("%s + 6 = 11", 5));
333     assertEquals("5 + 6 = 11", Preconditions.format("5 + %s = 11", 6));
334     assertEquals("5 + 6 = 11", Preconditions.format("5 + 6 = %s", 11));
335     assertEquals("5 + 6 = 11", Preconditions.format("%s + %s = %s", 5, 6, 11));
336     assertEquals("null [null, null]",
337         Preconditions.format("%s", null, null, null));
338     assertEquals("null [5, 6]", Preconditions.format(null, 5, 6));
339   }
340 
341   private static final Object IGNORE_ME = new Object() {
342     @Override public String toString() {
343       throw new AssertionFailedError();
344     }
345   };
346 
347   private static class Message {
348     boolean invoked;
349     @Override public String toString() {
350       assertFalse(invoked);
351       invoked = true;
352       return "A message";
353     }
354   }
355 
356   private static final String FORMAT = "I ate %s pies.";
357 
358   private static void verifySimpleMessage(Exception e) {
359     assertEquals("A message", e.getMessage());
360   }
361 
362   private static void verifyComplexMessage(Exception e) {
363     assertEquals("I ate 5 pies.", e.getMessage());
364   }
365 }